<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/10/13
 * Time: 22:07
 */
namespace app\model;


use think\facade\Db;
use think\Model;

class SignIn extends Model
{

    private static function _getListOutPutFields()
    {
        return [
            'sign_id as signId',
            'sign_uid as id',
            'sign_pic as pic',
            'pername',
            'oreaddress',
            'sign_time as signTime'
        ];
    }
    public function addOneSign($data){
        $data['sign_create_time'] = time();
        return Db::table('sign_in')->insertGetId($data);
    }

    public function getSignInTotalNumByUid($uid){
        $where = [
            'sign_uid' => $uid,
            'sign_status' => 1
        ];
        return Db::table('sign_in')->where($where)->count();
    }

    public function getSignAllCountByWhere($where,$group = 'sign_uid')
    {
        return Db::table('sign_in')->where($where)->group($group)->count();
    }

    public function getSignCountByWhere($where)
    {
        return Db::table('sign_in')->where($where)->count();
    }

    public function getSignInfoByWhere($where)
    {
        return Db::table('sign_in')->where($where)->find();
    }

    public function updateSignInfoById($signId,$data)
    {
        $data['sign_update_time'] = time();
        return Db::table('sign_in')->where(['sign_id'=>$signId])->save($data);
    }

    public function getSignListByWhere($where,$field = '')
    {
        $field = $field ? $field : self::_getListOutPutFields();
        return Db::table('sign_in')
            ->leftJoin('user','user.id=sign_uid')
            ->leftJoin('organization','organization.id=sign_orgid')
            ->where($where)
            ->field($field)
            ->order('sign_id desc')
            ->select();
    }

    public function getHeadSignInfoByWhere($where,$field = '')
    {
        $field = $field ? $field : self::_getListOutPutFields();
        return Db::table('sign_in')
            ->leftJoin('user','user.id=sign_uid')
            ->leftJoin('organization','organization.id=sign_orgid')
            ->where($where)
            ->field($field)
            ->order('sign_id desc')
            ->limit(1)
            ->find();
    }


    public function getSignPageListByWhere($where,$field = '')
    {
        $field = $field ? $field : self::_getListOutPutFields();
        return Db::table('sign_in')
            ->leftJoin('user','user.id=sign_uid')
            ->leftJoin('organization','organization.id=sign_orgid')
            ->where($where)
            ->field($field)
            ->order('sign_id desc')
            ->paginate(input('param.pageSize'))
            ->render();
    }
}